package com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.provider

import android.content.Context
import android.view.View
import com.example.library_common.adapter.multi.JssMultiItemViewHolder
import com.example.library_common.adapter.multi.JssNewMultipleAdapter
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleBannerEntity
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleBannerWrapperEntity
import com.example.wallet.global.AppGlobals
import com.youth.banner.Banner
import com.youth.banner.indicator.CircleIndicator
import java.util.*

class NewSaleBannerProvider(view: View?) :
    JssMultiItemViewHolder<NewSaleBannerWrapperEntity>(view) {

    private var height = 0

    init {
        val screenWidth = QMUIDisplayHelper.getScreenWidth(AppGlobals.getApplication())
        height = screenWidth / 2
    }

    override fun getItemLayout(): Int {
        return R.layout.new_sale_banner_provider
    }

    private var mAdapter: NewSaleBannerAdapter? = null

    override fun convert(context: Context, m: NewSaleBannerWrapperEntity?) {
        super.convert(context, m)

        val mBannerView: Banner<NewSaleBannerEntity, NewSaleBannerAdapter> =
            findView22(R.id.mBanner)
        val lp = mBannerView.layoutParams
        if (lp != null && lp.height != height) {
            lp.height = height
            mBannerView.layoutParams = lp
        }

        if (mAdapter == null) {
            mAdapter = NewSaleBannerAdapter(ArrayList(), context)

            mBannerView.adapter = mAdapter

            mBannerView.indicator = CircleIndicator(context)
            if (adapter is JssNewMultipleAdapter<*>) {
                mBannerView.addBannerLifecycleObserver((adapter as JssNewMultipleAdapter<*>).lifecycleOwner)
            }
        }
        if (mAdapter != null) {

            mAdapter?.addAll(m?.getData() as Collection<NewSaleBannerEntity>)
        }
    }

    override fun onViewAttachedToWindow() {
        super.onViewAttachedToWindow()
        val mBannerView: Banner<NewSaleBannerEntity, NewSaleBannerAdapter> =
            findView22(R.id.mBanner)
        mBannerView.start()
    }

    override fun onViewDetachedFromWindow() {
        super.onViewDetachedFromWindow()
        val mBannerView: Banner<NewSaleBannerEntity, NewSaleBannerAdapter> =
            findView22(R.id.mBanner)
        mBannerView.stop()
    }
}